using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.U2D;
using Yoozoo.Managers.ResourceManagerV2.Framework.Resource;
using Yoozoo.Managers.ResourceManagerV2.Runtime;
using Yoozoo.UI;
using Yoozoo.UI.YGUI;

public class SpriteRendererBinder : BinderBase
{
    public enum AttributeType : int
    {
        spriteName,
        materialName,
        enabled,
    }

    private SpriteRenderer _target;
    private ResLoader _resLoaderMaterial;
    private ResLoader _resLoaderTexture;
    
    private ResLoader _oldResLoaderMaterial;
    private ResLoader _oldResLoaderTexture;

    private string _spriteName;
    private string _materialName;

    private Sprite _defaultSprite;

    public SpriteRendererBinder(UIExpansion owner, LinkerConfig config) : base(owner, config)
    {
    }

    public SpriteRendererBinder()
    {
        
    }

    public override void Init(LinkerConfig config)
    {
        GameObject targetGo = _owner.GetStoredGameObject(config.StoredGameObjectIndex);
        if (targetGo == null)
        {
            return;
        }

        _target = targetGo.GetComponent<SpriteRenderer>();
        _active = _target != null;
        
        _defaultSprite = _target.sprite;
    }

    public override void SetString(string value)
    {
        switch ((AttributeType) Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.spriteName:

                if (_spriteName == value)
                    return;
                _spriteName = value;
                
                if (string.IsNullOrEmpty(value))
                {
                    _target.sprite = null;
                    _oldResLoaderTexture?.Recycle2Cache();
                    _oldResLoaderTexture = null;
                    _resLoaderTexture?.Recycle2Cache();
                    _resLoaderTexture = null;
                    return;
                }
                _oldResLoaderTexture = _resLoaderTexture;
                _resLoaderTexture = ResLoader.Alloc();
                _resLoaderTexture.Add2Load("Assets/ResourcesAssets/" + value + ".png", typeof(Sprite),
                    (success, name, asset) =>
                    {
                        _oldResLoaderTexture?.Recycle2Cache();
                        _oldResLoaderTexture = null;
                        if (!success)
                        {
                            _target.sprite = null;
                            UnityEngine.Debug.LogError("dont has this sprite:" + value);
                            _spriteName = null;
                        }
                        else
                        {
                            if (_target == null || asset == null)
                            {
                                Debug.LogError(value);
                            }
                            _target.sprite = asset as Sprite;
                        }
                    }).Load();
                break;
            case AttributeType.materialName:
                if (_materialName == value)
                    return;
                _materialName = value;
                
                if (string.IsNullOrEmpty(value))
                {
                    _target.material = null;
                    _oldResLoaderMaterial?.Recycle2Cache();
                    _oldResLoaderMaterial = null;
                    _resLoaderMaterial?.Recycle2Cache();
                    _resLoaderMaterial = null;
                    return;
                }
                _oldResLoaderMaterial = _resLoaderMaterial;
                
                _resLoaderMaterial = ResLoader.Alloc();
                _resLoaderMaterial.Add2Load("Assets/ResourcesAssets/" + value + ".mat",
                    (success, name, asset) =>
                    {
                        if (!success)
                        {
                            //_target.material = _target.defaultMaterial;
                            UnityEngine.Debug.LogError("dont has this material:" + value);
                        }
                        else
                        {
                            var mat = asset as Material;
                            _target.material = mat;
                            _materialName = value;
                        }
                        _oldResLoaderMaterial?.Recycle2Cache();
                        _oldResLoaderMaterial = null;
                    }).Load();
                break;
            default:
                break;
        }

        base.SetString(value);
    }

    public override void SetBoolean(bool value)
    {
        switch ((AttributeType) Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.enabled:
                _target.enabled = value;
                break;
            default:
                break;
        }

        base.SetBoolean(value);
    }
    
    public override void RemoveAllAction()
    {
        ResetOnUnBind();
    }

    private void ResetOnUnBind()
    {
        _spriteName = string.Empty;
        _materialName= string.Empty;
        _target.sprite = _defaultSprite;
        _resLoaderMaterial?.Recycle2Cache();
        _resLoaderMaterial = null;
        _resLoaderTexture?.Recycle2Cache();
        _resLoaderTexture = null;
        _oldResLoaderTexture?.Recycle2Cache();
        _oldResLoaderTexture = null;
        _oldResLoaderMaterial?.Recycle2Cache();
        _oldResLoaderMaterial = null;
    }

    public override void Dispose()
    {
        base.Dispose();
        ResetOnUnBind();
    }
}